package org.wevil.repo;

import org.wevil.entity.news.News;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface NewsRepo extends BaseRepo<News, Integer> {

    /** 统计发布量 */
    @Query("select count(publishTime) from News where publishTime is not null")
    Integer sumByPublish();

    @Query("select count(id) from News where newsStatus = 3")
    long countByPublish();



    /** 统计点击量 */
    @Query("select sum(click) from News")
    Integer sumByClick();

    /** 获取所有创建用户id */
    @Query("select distinct createId from News")
    List<Integer> findCreateId();

    @Modifying
    @Query("update News t set " +
            "t.subjectId = case when :#{#entity.subjectId} is null then t.subjectId else :#{#entity.subjectId} end, " +
            "t.coverId = case when :#{#entity.coverId} is null then t.coverId else :#{#entity.coverId} end, " +
            "t.title = case when :#{#entity.title} is null then t.title else :#{#entity.title} end, " +
            "t.content = case when :#{#entity.content} is null then t.content else :#{#entity.content} end, " +
            "t.href = case when :#{#entity.href} is null then t.href else :#{#entity.href} end, " +
            "t.affixIds = case when :#{#entity.affixIds} is null then t.affixIds else :#{#entity.affixIds} end, " +
            "t.newsStatus = case when :#{#entity.newsStatus} is null then t.newsStatus else :#{#entity.newsStatus} end, " +
            "t.newsSort = case when :#{#entity.newsSort} is null then t.newsSort else :#{#entity.newsSort} end, " +
            "t.top = case when :#{#entity.top} is null then t.top else :#{#entity.top} end, " +
            "t.click = case when :#{#entity.click} is null then t.click else :#{#entity.click} end, " +
            "t.createId = case when :#{#entity.createId} is null then t.createId else :#{#entity.createId} end, " +
            "t.updateId = case when :#{#entity.updateId} is null then t.updateId else :#{#entity.updateId} end, " +
            "t.auditId = case when :#{#entity.auditId} is null then t.auditId else :#{#entity.auditId} end, " +
            "t.publishId = case when :#{#entity.publishId} is null then t.publishId else :#{#entity.publishId} end, " +
            "t.createTime = case when :#{#entity.createTime} is null then t.createTime else :#{#entity.createTime} end, " +
            "t.updateTime = case when :#{#entity.updateTime} is null then t.updateTime else :#{#entity.updateTime} end, " +
            "t.auditTime = case when :#{#entity.auditTime} is null then t.auditTime else :#{#entity.auditTime} end, " +
            "t.publishTime = case when :#{#entity.publishTime} is null then t.publishTime else :#{#entity.publishTime} end, " +
            "t.ui = case when :#{#entity.ui} is null then t.ui else :#{#entity.ui} end, " +
            "t.ts = case when :#{#entity.ts} is null then t.ts else :#{#entity.ts} end " +
            "where t.id = :#{#entity.id}")
    int update(News entity);
}
